package com.lhy.ltss.service;

import com.lhy.ltss.common.OrderStatusConst;
import com.lhy.ltss.dao.FindDao;
import com.lhy.ltss.dao.OrderDao;
import com.lhy.ltss.entity.Book;
import com.lhy.ltss.entity.Order;
import com.lhy.ltss.entity.Page;
import com.lhy.ltss.session.UserManage;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: 李汇远
 */
@Service
@Transactional
public class FinderService {

    @Autowired
    UserManage userManage;

    @Autowired
    OrderDao orderDao;

    @Autowired
    FindDao findDao;

    /**
     * 获取当前找书员的订单
     * @return
     */
    public List<Order> getOrder(String  bookIndex){
        String username = userManage.getUsername();
        bookIndex += "%";
        return orderDao.getFinderOrder(bookIndex, username);
    }

    /**
     * 更改订单详情状态，并检查主订单的订单详情是否全部处理
     * @param id
     * @param status
     * @return
     */
    public void updateStatus(Integer id, String status){
        orderDao.updateOrderDetailStatus(status, id);
        findDao.remove(id);
        int orderId = orderDao.getParentOrderId(id);
        int num = orderDao.getNoFinishOrderDetailNum(orderId);
        if (num == 0){
            // 如果全部已完成，则修改状态
            int n1 = orderDao.getRegisterOfOrderNum(orderId);
            orderDao.updateOrderStatus(n1 == 0 ? OrderStatusConst.FAIL : OrderStatusConst.REGISTER, orderId);
        }
    }


}
